<?php
/**
 * @file : celebrityimage.php
 * @date : 2014-12-27
 */
namespace haibao\cms\data;

use haibao\cms\model\filter\CelebrityImage as CelebrityImageFilter;

class CelebrityImage extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\CelebrityImage');
    }

    /**
     * 添加明星图像
     * @param : Object \haibao\cms\model\data\CelebrityImage $model
     */
    public function addCelebrityImage($model){
        try{
           $this->add($model);
        } catch( \Exception $e){
            throw $e;
        }
    }
    
    /**
     * 根据明星ID获取明星图像（单个ID）
     * @param Int $celebrityId 明星ID
     */
    public function getCelebrityImageByCelebrityId($celebrityId){
    
    	$filter = new CelebrityImageFilter();
    	$filter->select(array(
    			CelebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_ID,
    			CelebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_TYPE,
    	));
    	$filter->where(CelebrityImageFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$celebrityId);
    	return $this->getAll($filter);
    }
    
    /**
     * 根据明星ID获取明星图像（多个ID）
     * @param Array $celebrityId 明星ID
     */
    public function getCelebrityImagesByIds($celebrityIds){

    	$filter = new CelebrityImageFilter();
    	$filter->select(array(
    		CelebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_ID,
    		CelebrityImageFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,
    		CelebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_TYPE,
    	));
    	
    	$filter->where(CelebrityImageFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,\haibao\frame\data\query\Condition::CONDITION_IN,$celebrityIds);

    	return $this->getAll($filter);
    }
    
    /**
     * 根据明星ID更新明星图片信息
     * @param : Object \haibao\cms\model\data\Celebrity
     */
    public function updateImageByCelebrityId($celebrityModel,$userName){
        $postImageTypeArr = array();
    	foreach($celebrityModel->CelebrityImage as $image){
    		$imageId = intval($image->ImageId);
    		$postImageTypeArr[] = $image->ImageType;
			
			$sql ="select ImageId,ImageType from `CelebrityImage` where CelebrityId=%s and ImageType=%s limit 1";
			$result = $this->query($sql, array($celebrityModel->Id,$image->ImageType));
			if($result->fetch_assoc()){
				
                if($result->getData('ImageId') != $imageId){
                    $this->updateOneImageStatus($image,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,$userName);
					$image->ImageId = $result->getData('ImageId');
					$this->updateOneImageStatus($image,\haibao\cms\model\data\ImageLibrary::STATUS_DISABLE,$userName);

					$sql = 'update `CelebrityImage` set ImageId = %s where CelebrityId=%s and ImageType=%s';
					$arrValue = array();
					array_push($arrValue, $imageId);
					array_push($arrValue, $celebrityModel->Id);
					array_push($arrValue, $image->ImageType);
					$this->query($sql, $arrValue);
					$image->ImageId = $imageId;
                }
			}else{
				$sql = 'insert into `CelebrityImage` (CelebrityId,ImageId,TagId,ImageType) values (%s,%s,%s,%s)';
				$this->query($sql,array($celebrityModel->Id, $imageId,$celebrityModel->TagId,$image->ImageType));
				$this->updateOneImageStatus($image,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,$userName);	
			}

    	}
        $model = new \haibao\cms\model\data\CelebrityImage();
    	$sysImageTypeArr = $model->getImageType();
    	
    	$diffArr = array_diff($sysImageTypeArr, $postImageTypeArr);
    	if($diffArr){
    		$sql = 'delete from `CelebrityImage` where CelebrityId=%s and ImageType in ('.implode(',',$diffArr).')';
    		$this->query($sql,array($celebrityModel->Id));
    	}
    } 
    /**
     * 通知图片模块更改明星所有的上传图片状态
     * @param ： Object \haibao\cms\model\data\Celebrity $celebrityModel
     * @param : Int \haibao\cms\model\data\ImageLibrary $status
     */
    public function updateAllImageStatus($celebrityModel, $status = \haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,$userName){
    	 
    	foreach($celebrityModel->CelebrityImage as $image){
    
    		$imageLibraryData = new \haibao\cms\data\ImageLibrary();
    		$imageLibraryData->updateImageStatus($image->ImageId,$status,$userName);
    	}
    }
    
    /**
     * 通过图片更新单一图片状态
     * @param ： Object \haibao\cms\model\data\CelebrityImage $celebrityImageModel
     * @param : Int \haibao\cms\model\data\ImageLibrary $status
     */
    public function updateOneImageStatus($celebrityImageModel, $status = \haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,$userName){

    		$imageLibraryData = new \haibao\cms\data\ImageLibrary();
    		$imageLibraryData->updateImageStatus(intval($celebrityImageModel->ImageId),$status,$userName);
    }
}
